package com.app.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 作者：lblbh on 2017/3/2 10:19
 * 邮箱：lanbuhan@163.com
 * 功能：首页使用 宽度1 高度0.85
 */
public class ItemImageView extends ImageView {

    private static double ratio = 1.3333;
    public ItemImageView(Context context) {
        super(context);
    }

    public ItemImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        float ratio_bl = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res-auto"
                , "img_bl", 0f);
        if(ratio_bl > 0){
            ratio = ratio_bl;
//            SystemPrintl.systemPrintl("当前页面我的图片比例是--->"+ratio);
            invalidate();
        }
    }

    public ItemImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ItemImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        float ratio_bl = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res-auto"
                , "img_bl", 0f);
        if(ratio_bl > 0){
            ratio = ratio_bl;
            invalidate();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);

        if(ratio!=0){
            double height = width / ratio;
            heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) height,MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
